package com.godpaper.twoHitOne.busniess.managers
{
	import com.godpaper.twoHitOne.model.BoardModel;
	import com.godpaper.twoHitOne.model.PiecesModel;
	import com.godpaper.twoHitOne.model.PlayerModel;
	import com.godpaper.twoHitOne.views.components.PieceButton;
	import com.godpaper.twoHitOne.vo.PieceVO;
	
	import mx.collections.ArrayCollection;
	
	public class BoardManager
	{
		public function BoardManager()
		{
		}
		
		
		public static function repopulation():void
		{
			//reset all pieces and gaskets
			PiecesModel.getInstance().selectedPiece.selected = false;
			PiecesModel.getInstance().selectedPiece = null;
		}
		
		//whether game phases ended and some pieces beKilled.
		public static function analysisPhases(reds:ArrayCollection,blues:ArrayCollection):Boolean
		{
			trace("#####################analysisGame begin##########################");
			if(reds.length==0)
			{
				PlayerManager.computerWin();
			}else if(blues.length==0)
			{
				PlayerManager.humanWin();
			}else
			{
				trace("	PiecesModel.getInstance().selectedPiece.name:",PiecesModel.getInstance().selectedPiece.name);
				//				PiecesModel.getInstance().selectedPiece.pieceVO.position[0];
				//horizontal analysis
				horizontalAnalysis(
					BoardModel.getInstance().gamePosition.getCol(
						PiecesModel.getInstance().selectedPiece.pieceVO.position[0]
																 ));
				//vertical analysis
				verticalAnalysis(
					BoardModel.getInstance().gamePosition.getRow(
						PiecesModel.getInstance().selectedPiece.pieceVO.position[1]
																 ));
				
				trace("#####################analysisGame end############################");
			}	
			return (reds.length==0 || blues.length==0);
		}
		//Notify:horizontalAnalysis must be first;
		/**
		 * 
		 * @param dataProvider,gamePosition getCol().
		 * @param onlyForAnalysis, determine whether analysis gamePhases for gameAI.
		 * @return horizontally the number of pieces can be killed.
		 * 
		 */		
		public static function horizontalAnalysis(dataProvider:Array,onlyForAnalysis:Boolean=false):ArrayCollection
		{
			trace("	#####################horizontalAnalysis begin################");
			////////////////////////////////////////////////////
			//////////////////about horizontal analysis//////////
			//////////////////////////////////////////////////
			//do staff:
			
			var hCounter:int = 4;
			var position:int;
			var redsCounter:int=0;
			var bluesCounter:int=0;
			var reds:ArrayCollection = new ArrayCollection();
			var blues:ArrayCollection = new ArrayCollection();
			var beKilled:ArrayCollection = new ArrayCollection();
			//filter 1: only 3 pieces in a line;
			for(var i:int=0;i<dataProvider.length;i++)
			{
				if(dataProvider[i]==null)
				{
					hCounter--;
					if(hCounter==3)
					{
						trace("		#match filter 1(only 3 pieces in a line)#");
						position = i;
					}
				}else
				{
					if(dataProvider[i].pieceVO.name.indexOf(PieceButton.NAME_PREFIX_RED)!=-1)
					{
						redsCounter++;
						reds.addItem(dataProvider[i]);
					}else
					{
						bluesCounter++;
						blues.addItem(dataProvider[i]);
					}
				}
			}
			
			//filter 2: only sequential pieces in a line;
			if((hCounter==3)&&(position%4==0)||(position%4==3))
			{
				trace("		#match filter 2(only sequential pieces in a line)#");
				var pieceVO:PieceVO = new PieceVO();
				if(redsCounter==1)
				{
					trace("		#match filter 3(only [1red&2blue])#");
					if( (blues.getItemAt(0) as PieceButton).pieceVO.position[1]+1 == (blues.getItemAt(1) as PieceButton).pieceVO.position[1])
					{
						trace("		#match filter 4(only [1blue,1blue,1red])#");
						if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
						{
							trace("		#match filter 5(only 1red can beKilled)#");
							trace("		 willBeKilled:reds.getItemAt(0):",reds.getItemAt(0));
							pieceVO = reds.getItemAt(0).pieceVO;
							if(!onlyForAnalysis)
							{
								pieceVO.willBeKilled = true;
							}
							reds.getItemAt(0).pieceVO = pieceVO;//transfor to willBeKilled func.
							beKilled.addItem( reds.getItemAt(0) as PieceButton );//store beKilled PieceButton
						}
					}
				}
				else if(bluesCounter==1)
				{
					trace("		#match filter 3(only [1blue&2red])#");
					if( (reds.getItemAt(0) as PieceButton).pieceVO.position[1]+1 == (reds.getItemAt(1) as PieceButton).pieceVO.position[1])
					{
						trace("		#match filter 4(only [1red,1red,1blue])#");
						if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_RED_TURN_NOW)
						{
							trace("		#match filter 5(only 1blue can beKilled)#");
							trace("		 willBeKilled:blues.getItemAt(0):",blues.getItemAt(0));
							pieceVO = blues.getItemAt(0).pieceVO;
							if(!onlyForAnalysis)
							{
								pieceVO.willBeKilled = true;
							}
							blues.getItemAt(0).pieceVO = pieceVO;//transfor to willBeKilled func.
							beKilled.addItem( blues.getItemAt(0) as PieceButton);//store beKilled PieceButton
						}
					}	
				}else{};
			}
			trace("	#####################horizontalAnalysis end##################");
			return beKilled;
		}
		//Notify:horizontalAnalysis must be first;
		/**
		 * 
		 * @param dataProvider,gamePosition getRow().
		 * @param forAnalysis, determine whether analysis gamePhases for gameAI.
		 * @return vertically the number of pieces can be killed.
		 * 
		 */		
		public static function verticalAnalysis(dataProvider:Array,onlyForAnalysis:Boolean=false):ArrayCollection
		{
			trace("	#####################verticalAnalysis begin##################");
			//////////////////////////////////////////////////				
			//////////////////about vertical analysis////////////////
			//////////////////////////////////////////////////
			//do staff
			var vCounter:int = 4;
			var position:int;
			var redsCounter:int=0;
			var bluesCounter:int=0;
			var reds:ArrayCollection = new ArrayCollection();
			var blues:ArrayCollection = new ArrayCollection();
			var beKilled:ArrayCollection = new ArrayCollection();
			//filter 1: only 3 pieces in a line;
			for(var i:int=0;i<dataProvider.length;i++)
			{
				if(dataProvider[i]==null)
				{
					vCounter--;
					if(vCounter==3)
					{
						trace("		#match filter 1(only 3 pieces in a line)#");
						position = i;
					}
				}else
				{
					if(dataProvider[i].pieceVO.name.indexOf(PieceButton.NAME_PREFIX_RED)!=-1)
					{
						redsCounter++;
						reds.addItem(dataProvider[i]);
					}else
					{
						bluesCounter++;
						blues.addItem(dataProvider[i]);
					}
				}
			}
			
			//filter 2: only sequential pieces in a line;
			if((vCounter==3)&&(position%4==0)||(position%4==3))
			{
				trace("		#match filter 2(only sequential pieces in a line)#");
				var pieceVO:PieceVO = new PieceVO();
				if(redsCounter==1)
				{
					trace("		#match filter 3(only [1red&2blue])#");
					if( (blues.getItemAt(0) as PieceButton).pieceVO.position[0]+1 == (blues.getItemAt(1) as PieceButton).pieceVO.position[0])
					{
						trace("		#match filter 4(only [1blue,1blue,1red])#");
						if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
						{
							trace("		#match filter 5(only 1red can beKilled)#");
							trace("		 willBeKilled:reds.getItemAt(0):",reds.getItemAt(0));
							pieceVO = reds.getItemAt(0).pieceVO;
							if(!onlyForAnalysis)
							{
								pieceVO.willBeKilled = true;
							}
							reds.getItemAt(0).pieceVO = pieceVO;//transfor to willBeKilled func.
							beKilled.addItem( reds.getItemAt(0) as PieceButton );//store beKilled PieceButton
						}
					}	
				}
				else if(bluesCounter==1)
				{
					trace("		#match filter 3(only [1blue&2red])#");
					if( (reds.getItemAt(0) as PieceButton).pieceVO.position[0]+1 == (reds.getItemAt(1) as PieceButton).pieceVO.position[0])
					{
						trace("		#match filter 4(only [1red,1red,1blue])#");
						if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_RED_TURN_NOW)
						{
							trace("		#match filter 5(only 1blue can beKilled)#");
							trace("		 willBeKilled:blues.getItemAt(0):",blues.getItemAt(0));
							pieceVO = blues.getItemAt(0).pieceVO;
							if(!onlyForAnalysis)
							{
								pieceVO.willBeKilled = true;
							}
							blues.getItemAt(0).pieceVO = pieceVO;//transfor to willBeKilled func.
							beKilled.addItem( blues.getItemAt(0) as PieceButton );//store beKilled PieceButton
						}
					}	
				}else{};
			}
			trace("	#####################verticalAnalysis end####################");
			BoardManager.repopulation();
			return beKilled;
		}
	}
}